package com.jack.tang.juc.demos;


public class saleTicket {
    public static void main(String[] args) {
        //一份资源
        Ticket ticket = new Ticket();
        //多个线程
        new Thread(() -> {
            for (int i = 0; i < 30; i++) {
                ticket.sale();
            }
        }, "A").start();
        new Thread(() -> {
            for (int i = 0; i < 20; i++) {
                ticket.sale();
            }
        }, "B").start();
        new Thread(() -> {
            for (int i = 0; i < 80; i++) {
                ticket.sale();
            }
        }, "C").start();
    }
}

//资源类
class Ticket {
    //票数
    private int number = 100;

    public synchronized void sale() {
        if (number > 0){
            System.out.println(Thread.currentThread().getName() + "卖出了第" + (number--) + "张票,剩余" + number);
        }else
            System.out.println(Thread.currentThread().getName() + "想卖但是没票了,剩余" + number);
    }
}

